﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Collections;

using YT.DB;
using YT.Commons;
using YT.Modules;

namespace YT.XH.IgE
{
    public class PrintDgnosisDAL
    {
        public bool printDgnosisDAL(DBAccess dbcon, string strSickRecID)
        {
            DataFunctions.DelTemporyTable(dbcon, "#tmpDetectInfo");
            string strSQL = @"Create Table #tmpDetectInfo ( 
                             RecID int identity(1,1), 
                             SickRecID char(16) not null default 0,                             
                             SickName char(20) not null default '',
                             Sex char(5) not null default '',
                             Age int not null default '',
                             CheckName varchar(10) not null default '',   
                             CheckDate1 DateTime NULL,                             
                             CalResult1 decimal(18,8) NULL,
                           
                             CheckDate2 DateTime NULL,                             
                             CalResult2 decimal(18,8) NULL,
                             CheckType char(20) not null default '',
                             Remark varchar(500) not null default '')";
            dbcon.ExecuteSql(strSQL);

            //            strSQL = @"Insert Into #tmpDetectInfo (SickRecID,SickName,CheckDate1,CalResult1,CheckType)
            //                       Values ('{0}','{1}','{2}','{3}','{4}')";
            //            strSQL = string.Format(strSQL, "1111222233334444", "99", "2013-8-22", 22, "IgE");
            //            dbcon.ExecuteSql(strSQL);
            //            strSQL = @"Insert Into #tmpDetectInfo (SickRecID,SickName,CheckDate1,CalResult1,CheckType)
            //                       Values ('{0}','{1}','{2}','{3}','{4}')";
            //            strSQL = string.Format(strSQL, "1111222233334444", "99", "2013-8-23", 44, "IgE");
            //            dbcon.ExecuteSql(strSQL);
            //            strSQL = @"Insert Into #tmpDetectInfo (SickRecID,SickName,CheckDate1,CalResult1,CheckType)
            //                       Values ('{0}','{1}','{2}','{3}','{4}')";
            //            strSQL = string.Format(strSQL, "1111222233334444", "99", "2013-8-24", 66, "IgE");
            //            dbcon.ExecuteSql(strSQL);
            //            strSQL = @"Insert Into #tmpDetectInfo (SickRecID,SickName,CheckDate1,CalResult1,CheckType)
            //                       Values ('{0}','{1}','{2}','{3}','{4}')";
            //            strSQL = string.Format(strSQL, "1111222233334444", "99", "2013-8-25", 88, "IgE");
            //            dbcon.ExecuteSql(strSQL);
            //            strSQL = @"Insert Into #tmpDetectInfo (SickRecID,SickName,CheckDate1,CalResult1,CheckType)
            //                       Values ('{0}','{1}','{2}','{3}','{4}')";
            //            strSQL = string.Format(strSQL, "1111222233334444", "99", "2013-8-26", 66, "IgE");
            //            dbcon.ExecuteSql(strSQL);
            //            strSQL = @"Insert Into #tmpDetectInfo (SickRecID,SickName,CheckDate1,CalResult1,CheckType)
            //                       Values ('{0}','{1}','{2}','{3}','{4}')";
            //            strSQL = string.Format(strSQL, "1111222233334444", "99", "2013-8-27", 22, "IgE");
            //            dbcon.ExecuteSql(strSQL);


            //            strSQL = @"Insert Into #tmpDetectInfo (SickRecID,SickName,CheckDate2,CalResult2,CheckType)
            //                       Values ('{0}','{1}','{2}','{3}','{4}')";
            //            strSQL = string.Format(strSQL, "222233334444", "99", "2013-8-22", 122, "TotIgE");
            //            dbcon.ExecuteSql(strSQL);
            //            strSQL = @"Insert Into #tmpDetectInfo (SickRecID,SickName,CheckDate2,CalResult2,CheckType)
            //                       Values ('{0}','{1}','{2}','{3}','{4}')";
            //            strSQL = string.Format(strSQL, "222233334444", "99", "2013-8-24", 144, "TotIgE");
            //            dbcon.ExecuteSql(strSQL);
            //            strSQL = @"Insert Into #tmpDetectInfo (SickRecID,SickName,CheckDate2,CalResult2,CheckType)
            //                       Values ('{0}','{1}','{2}','{3}','{4}')";
            //            strSQL = string.Format(strSQL, "222233334444", "99", "2013-8-30", 166, "TotIgE");
            //            dbcon.ExecuteSql(strSQL);
            //            strSQL = @"Insert Into #tmpDetectInfo (SickRecID,SickName,CheckDate2,CalResult2,CheckType)
            //                       Values ('{0}','{1}','{2}','{3}','{4}')";
            //            strSQL = string.Format(strSQL, "222233334444", "99", "2013-8-31", 188, "TotIgE");
            //            dbcon.ExecuteSql(strSQL);
            //            strSQL = @"Insert Into #tmpDetectInfo (SickRecID,SickName,CheckDate2,CalResult2,CheckType)
            //                       Values ('{0}','{1}','{2}','{3}','{4}')";
            //            strSQL = string.Format(strSQL, "222233334444", "99", "2013-9-01", 166, "TotIgE");
            //            dbcon.ExecuteSql(strSQL);
            //            strSQL = @"Insert Into #tmpDetectInfo (SickRecID,SickName,CheckDate2,CalResult2,CheckType)
            //                       Values ('{0}','{1}','{2}','{3}','{4}')";
            //            strSQL = string.Format(strSQL, "222233334444", "99", "2014-9-02", 122, "TotIgE");
            //            dbcon.ExecuteSql(strSQL);
            string strSickName = "";
            strSQL = "SELECT A.CheckDate,A.CheckName,B.SickPersonID,B.CALValue,A.FormulaType From IgEDetectHdr A INNER JOIN IgEDetectDetail B on A.RecID=B.HdrRecID where B.SickPersonID='" + strSickRecID + "' Order By A.CheckDate";
            DataTable dtHist = dbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtHist.Rows)
            {
                if (CvtFunc.ToString(drRow["FormulaType"]) == FormulaType.IgE.ToString())
                {
                    strSQL = @"Insert Into #tmpDetectInfo (SickRecID,SickName,CheckName,CheckDate1,CalResult1,CheckType)
                       Values ('{0}','{1}','{2}','{3}','{4}','{5}')";
                }
                else if (CvtFunc.ToString(drRow["FormulaType"]) == FormulaType.IgG4.ToString())
                {
                    strSQL = @"Insert Into #tmpDetectInfo (SickRecID,CheckName,SickName,CheckDate2,CalResult2,CheckType)
                       Values ('{0}','{1}','{2}','{3}','{4}','{5}')";
                }
                else
                {
                    break;
                }

                strSickName = CvtFunc.ToString(dbcon.GetValue("select sickname from mSickPerson where SickRecID='" + strSickRecID + "'"));
                strSQL = string.Format(strSQL, strSickRecID
                                                , CvtFunc.ToString (drRow["CheckName"])
                                                , strSickName
                                                , BaseFunctions.FormatDate(drRow["CheckDate"].ToString(), DateFormatOption.DateFmtMDY)
                                                , CvtFunc.ToDouble(drRow["CALValue"])
                                                , CvtFunc.ToString(drRow["FormulaType"])
                                       );
                dbcon.ExecuteSql(strSQL);

                strSQL = "Update #tmpDetectInfo Set Sex=B.Sex, Age=B.Age from mSickPerson B where #tmpDetectInfo.SickRecID=B.SickRecID";
                dbcon.ExecuteSql(strSQL);
            }

            return true;
        }
    }
}
